package com.sea.base.ui

import android.app.Activity
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import com.sea.base.activities.AutoIUIActivity
import com.sea.base.activities.BaseKtActivity
import com.sea.base.ext.global.intentNew
import com.sea.base.fragment.FragmentVisibilityChild
import com.sea.base.fragment.opt.FragmentItem
import com.sea.base.fragment.opt.FragmentItemParent

object GlobalActivityUtil {

    /**
     * 跳转到预埋的activity，主要用于热修复打开新页面
     * @param T 需要展示的Fragment，Fragment所在的activity是BaseFullscreenKtActivity，属性均为默认，如果想自定义主题样式可以强制转换为该Activity
     */
    inline fun <reified T> startEmbedActivity(
        ui: IUIContext,
        arguments: Bundle? = null,
    ) where T : Fragment, T : com.sea.base.fragment.FragmentVisibilityChild {
        startEmbedActivity(ui, T::class.java, arguments)
    }

    inline fun <reified T : FragmentItem> startEmbedActivityByFragmentItem(
        ui: IUIContext,
        arguments: Bundle? = null,
    ) {
        startEmbedActivity(ui, FragmentItemParent::class.java, FragmentItem.createArguments<T>(arguments))
    }

    fun <T> startEmbedActivity(
        ui: IUIContext,
        clazz: Class<T>,
        arguments: Bundle? = null,
    ) where T : Fragment, T : com.sea.base.fragment.FragmentVisibilityChild {
        ui.startActivity(createEmbedIntent(clazz, arguments))
    }

    /**
     * 创建预埋的activity的intent
     * @param T 需要展示的Fragment，Fragment所在的activity是BaseFullscreenKtActivity，属性均为默认，如果想自定义主题样式可以强制转换为该Activity
     */
    inline fun <reified T> createEmbedIntent(
        arguments: Bundle? = null,
    ) where T : Fragment, T : com.sea.base.fragment.FragmentVisibilityChild =
        createEmbedIntent(T::class.java, arguments)

    fun <T> createEmbedIntent(
        clazz: Class<T>,
        arguments: Bundle? = null,
    ) where T : Fragment, T : com.sea.base.fragment.FragmentVisibilityChild =
        intentNew<com.sea.base.activities.EmbedActivity>()
            .putExtra("fragClassName", clazz.name)
            .putExtra("arguments", arguments)

    /**
     * 目前为回调方式，所以不支持被杀死后重建
     * 在透明activity打开后做某事
     * 注意：不用后请自行finish掉
     *
     * 使用场景：
     * 全局弹窗（不会因activity突然关闭而被关闭）
     * 拉起app（通知栏回到app）
     */
    fun doOnTransparentActivityVisible(callback: (AutoIUIActivity) -> Unit) {
        com.sea.base.activities.TransparentActivity.doOnVisible(callback)
    }

    fun isTransparentActivity(ui: IUIContext) = ui is com.sea.base.activities.TransparentActivity
}